package com.hopu.blb_erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hopu.blb_erp.entity.PageEntity;
import com.hopu.blb_erp.entity.TCargo;
import com.hopu.blb_erp.service.CargoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;

@Api
@RestController
public class CargoController {

    @Autowired
    //
    private CargoService cargoService;

    @ApiOperation("分页查询货位")
    @GetMapping("/cargos/{current}/{pageSize}")
    public ResponseEntity<PageEntity> findPage(@PathVariable("current")Integer current, @PathVariable("pageSize")Integer pageSize,
                                               String shelvesNo,String cargoNo,String status){
        PageEntity<TCargo> page = cargoService.findPage(current, pageSize,shelvesNo,cargoNo,status);
        return ResponseEntity.ok(page);
    }

    @ApiOperation("通过货架编号查询货位")
    @GetMapping("/cargos/{shelvesNo}")
    public ResponseEntity<List<TCargo>> findByNo(@PathVariable("shelvesNo")String shelvesNo){
        List<TCargo> cargos = cargoService.list(new QueryWrapper<TCargo>().lambda().eq(TCargo::getShelvesNo,shelvesNo));
        return ResponseEntity.ok(cargos);
    }

    @ApiOperation("添加货位")
    @PostMapping("/cargo")
    public ResponseEntity<String> add(@RequestBody TCargo tCargo){
        String s = cargoService.addTCargo(tCargo);
        if (s == "ok"){
            return ResponseEntity.ok("ok");
        }else {
            return ResponseEntity.ok("no");
        }

    }

    @ApiOperation("删除货位")
    @DeleteMapping("/cargo/{id}")
    public ResponseEntity<String> delete(@PathVariable("id") Long id){
        cargoService.removeById(id);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation("更新货位")
    @PutMapping("/cargo")
    public ResponseEntity<String> modify(@RequestBody TCargo tCargo){
        cargoService.updateById(tCargo);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation("修改货位状态")
    @PutMapping("/cargo/{id}/{status}")
    public ResponseEntity<String> update(@PathVariable("id") Integer id,@PathVariable("status")boolean status){
        TCargo tCargo = new TCargo();
        tCargo.setId(id);
        tCargo.setStatus(status);
        cargoService.updateById(tCargo);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation("通过商品名和仓库id查询货位")
    @GetMapping("/cargo/{productName}/{menuNo}")
    public ResponseEntity<List<TCargo>> findByName(@PathVariable("productName")String productName, @PathVariable("menuNo")String menuNo){
        //
        List<TCargo> cargos = cargoService.findByName(productName,menuNo);
        return ResponseEntity.ok(cargos);
    }
}
